#include "TextFingerPrintIterator.h"
#include "TextWindowIterator.h"


TextFingerPrintIterator::TextFingerPrintIterator(TextWindowIterator *input, int radix, HashValue_t q) {
	this->input_iterator = input;
	this->m = input->win_length;
	this->r = radix;
	this->q = q;

//	this->h = modpower(radix, m-1, q);
this->h = modradpow64(radix, m-1, q);
    //fprintf(stdout, "radix = %d, h= %d, %d, %d", this->r, this->h, sizeof this->h, sizeof this->r );
    //getc(stdin);
	this->text_finger_print = NULL;
	if(! input->eof) {
		this->fingerprint = hash_value((**input).substring, 0, m, r, q);
		this->text_finger_print = new TextFingerPrint(this->fingerprint, 0);
	}
}

TextFingerPrintIterator::~TextFingerPrintIterator(void) {
	if(this->text_finger_print)
		delete this->text_finger_print;
}

TextFingerPrintIterator& TextFingerPrintIterator::operator++() {
	int char_old = (**input_iterator).head() ;
	(*input_iterator)++;
	int char_new = (**input_iterator).tail() ;
	this->fingerprint = incr_hash_value(this->fingerprint, char_old, char_new, h, r, q);
	return (*this);
}

TextFingerPrint& TextFingerPrintIterator::operator*() {
	this->text_finger_print->file_position = (**input_iterator).file_position;
	this->text_finger_print->fingerprint = this->fingerprint;
	return *(this->text_finger_print);
}

bool TextFingerPrintIterator::operator==(const TextFingerPrintIterator other) {
	return (*input_iterator) == (*other.input_iterator);
}

bool TextFingerPrintIterator::operator!=(const TextFingerPrintIterator other) {
	return ! (*this == other);
}

TextFingerPrintIterator TextFingerPrintIterator::end(TextWindowIterator *end_iterator) {
	return TextFingerPrintIterator(end_iterator, 0, 0);
}